package com.whj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whj.entity.SysUser;
import com.whj.mapper.UserDao;
import com.whj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    @Override
    //登录
    public SysUser selectUser(String userName, String password) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        SysUser user = userDao.selectOne(queryWrapper);
        if (user != null && passwordEncoder.matches(password, user.getPassword())) {
            return user;
        }
        return null;
    }

    @Override
    public void insertUser(SysUser user) {
        userDao.insert(user);
    }
    @Override
    public Boolean ishave(SysUser user) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", user.getUserName());
        userDao.selectOne(queryWrapper);
        if (userDao.selectOne(queryWrapper) != null) {
            return false;
        }
        return true;
    }
    @Override
    public SysUser selectUserById(Integer id) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        return userDao.selectOne(queryWrapper);
    }

    @Override
    public List<SysUser> showZhuanJia() {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", "3");
        return userDao.selectList(queryWrapper);
    }
}
